<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mobile extends CI_Controller {    
    private $content='';
    private $mensenger='';    
    
    private $total_rows = 100;
    private $per_page =20;
    
    function  __construct(){
        parent::__construct();  
        $this->load->model('Admin_model');
        $this->load->library('form_validation');     
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>'); 
    }
    function index(){
        if(!$this->session->userdata('logedin')){            
            $data ['content']=$this->load->view('content/login','',true);           
            $this->load->view('index',$data);
        }else{
            redirect('mobile/account');
        }        
        
    }
    
    function login(){
        if(!$this->session->userdata('logedin')){           
    	   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
           $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');      
           if ($this->form_validation->run() == TRUE){            
                $username = $this->security->xss_clean($_POST['username']);
                $password = $this->security->xss_clean($_POST['password']);
                $password = sha1(md5($password));
                $log = $this ->Admin_model->get_one('users',array('username'=>$username,'password'=>$password));
                $num = $this ->Admin_model->get_number('users',array('username'=>$username,'password'=>$password));
                //get_number   
                //0 la pending 1 la approved 3 la banned        
                if($num==1){ 
                   /// if($num->banned==1){
                        $this->session->set_userdata('logedin',1);
                        $this->session->set_userdata('user',$log); 
                        redirect(base_url().'mobile/account');
                    //}else{
                      //  $this->session->set_userdata('loidn','Acount is pendding!');
                   // }
                                                   
                }else{                
                    $this->session->set_userdata('loidn','Incorrect login');
                }
           }                   
          
          $data ['content']=$this->load->view('content/login','',true);           
          $this->load->view('index',$data); 
        }else{            
            redirect('mobile/account');
        }        
       
    }
    
    function register(){        
        if(!$this->session->userdata('logedin')){        
            $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[25]|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[5]|max_length[25]|xss_clean');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
            if ($this->form_validation->run() == FALSE){
    		//	$this->session->set_userdata('loidn',1);
    		}else{
    		 	$data =$this->security->xss_clean($_POST);
                //$data['mailling']= serialize($data['mailling']);
                $data['ip']=$this->input->ip_address();
                $data['password']=sha1(md5($data['password']));
                $data['banned']=1;                
                unset($data['term']);           
                if(!$this->check_user($data['username'],$data['email'],$data['password'])){
                    $this->db->insert('users',$data); 
                    $log->id=$this->db->insert_id();
                    //chenn them vao bang stast
                    $this->db->where('id',1)->set('mem','mem + 1', false)->update('stast');
                    
                    $this->session->set_userdata('logedin',1);
                    $this->session->set_userdata('user',$log); 
                    //$this->session->set_userdata('loidn','You have successfully registered!');
                    redirect('mobile/acount');               
                }else{
                    $this->session->set_userdata('loidn',$this->mensenger);
                }
                
                
    		}
            $data ['content']=$this->load->view('mobile/content/register','',true);           
            $this->load->view('index',$data);   
        }else{
            redirect('mobile/account');
        } 
        
    } 
    function payout()
    {
        if($this->session->userdata('logedin')){
            $this->session->unset_userdata('loi');
            $account = $this ->Admin_model->get_one('users',array('id'=>$this->session->userdata('user')->id));
            //username,status,user_id
            // wall ->1 \\\ banner-> 2            
            if(!empty($_POST)){               
                $this->form_validation->set_rules('point', 'Point', 'trim|numeric|required|xss_clean');
                $this->form_validation->set_rules('accpay', 'Paypal', 'trim|required|xss_clean');
                $data = $this->security->xss_clean($_POST);// du lieu gui ve tu form
                if ($this->form_validation->run() == TRUE){ 
                    if($account->curent < $this->config->item('mini_payout')){                    
                        $this->session->set_userdata('loi','<b style="color:red;">&nbsp;&nbsp;&nbsp;Sorry, but the minimum cashout balance at this time is 0.2$.</b>');
                    }elseif($account->curent>0){                    
                        if($data['point']>=$account->curent){// check point cash voi point tai khoan neu no nhap lon hon thi cho bang
                            $data['point']=$account->curent;
                        }
                        $data['method'] = $data['method'];
                        $data['ip'] = $this->input->ip_address();
                        $data['status']='Pending';
                        $data['iduser'] = $account->id;                        
                        
                        $this->db->insert('payout', $data); 
                        $point = $account->curent - $data['point'];
                        $this->db->where('id', $account->id);
                        $this->db->update('users', array('curent'=>$point)); 
                        $this->session->set_userdata('loi','Success!');
                    }else{
                        $this->session->set_userdata('loi','Current point = 0 !');
                    }
                }
            }            
            $dta ['content'] = $this->load->view('mobile/content/payout',
                array(
                    'pay'=>$this->Home_model->get_data('payout',array('iduser' => $account->id,'checked'=>0))
                ),true
            );                   
        }else {            
            $dta ['content'] = 'Not login';
             
        }
        $this->load->view('index',$dta);
        
    }   
    function account(){
        if($_POST){
            $pass = $this->input->post('curentpass');
            $password = sha1(md5($pass));
            $log = $this ->Admin_model->get_one('users',array('id'=>$this->session->userdata('user')->id,'password'=>$password));
            if(empty($log)){
                $this->session->set_userdata('chagepass','The Password incorrect');
                $this->session->set_userdata('ok','0');
            }else{
                $this->db->where('id',$this->session->userdata('user')->id);
                $this->db->update('users',array('password'=>sha1(md5($this->input->post('newpass',true)))));
                $this->session->set_userdata('chagepass','The Password changed');
                $this->session->set_userdata('ok','1');
            }    
        }
        
        $data ['content']=$this->load->view('mobile/content/account',array('account'=>$this ->Admin_model->get_one('users',array('id'=>$this->session->userdata('user')->id)) ),true);           
        $this->load->view('index',$data);
        
    }
    function logout(){
        $this->session->unset_userdata('logedin');
        $this->session->unset_userdata('user'); 
        redirect(base_url().'mobile');
    }
    function check_user($username,$email,$pass){        
        if($this->Admin_model->check_trung(array('username'=>$username),'users')||
        $this->Admin_model->check_trung(array('email'=>$email),'users') ){
            if($this->Admin_model->check_trung(array('username'=>$username),'users')){
                $this->mensenger.='Username already exists</br>';                
            }
            if($this->Admin_model->check_trung(array('email'=>$email),'users')){
                $this->mensenger.='Email already exists</br>';                
            }
            return true;// la loi
        }else{
            return false;
        }
    }
    function banner($detail=0,$id=0){// offer banner
        if($this->session->userdata('logedin')){
            if($detail)
            {
                //chi tiet offer
               if(!is_numeric($id)){$id=0;}
                $off= $this->Home_model->get_one('offer',array('id'=>$id)); 
                if(!empty($off)){
                    $this->db->insert('tracklink',
                                        array(
                                        'tracklink'=>'no',
                                        'id_user'=>$this->session->userdata('user')->id,
                                        'id_offer'=>$off->id
                                        )
                                        );
                    $idi=$this->db->insert_id();
                    $track= dechex($idi);  
                    $this->db->where('id',$idi);
                    $this->db->update('tracklink',array('tracklink'=>$track));
                    $url= $off->url.$off->subid.$track;
                     redirect($url);
                    
                }else{
                    echo 'Offer doesn\'t exist';
                } 
                //end chi tiet offer
            }
            else
            {
                $data['content'] = $this->load->view('mobile/offer/c_banner',
                            array(
                                'offer'=>$this->Home_model->get_data('offer',array('show'=>1),array(),array('title','ASC'),'',array('id','title','img','description','point_rate'))
                            ),true);
                $this->load->view('index',$data);
            }
            
            //////// chi tieng offer
            
        }else redirect(base_url().'mobile/login');
    }
    function wall($detail=0,$id=0){
        if($this->session->userdata('logedin')){
            if($detail){
                $net = $this->Home_model->get_one('network',array('id'=>$id));
                $netscript = str_replace('[{userid}]',$this->session->userdata('user')->id,$net->script);
                $netscript = str_replace('[{email}]',$this->session->userdata('user')->email,$netscript);
                $netscript = str_replace('[{username}]',$this->session->userdata('user')->username,$netscript);
                $data['content'] = $netscript;                               
            }else{
                $data['content'] = $this->load->view('mobile/offer/c_wall',
                        array(
                            'net'=>$this->Home_model->get_data('network',array('show'=>1,'type'=>'Wall'),array(),array('order','DESC'),'',array('id','title','script'))
                        ),true);            
                
            }
            $this->load->view('index',$data);
            
        }else redirect(base_url().'mobile/login');
    }
    ////////////////history payout and history credit
    function history($type=0){
         if($this->session->userdata('logedin')){
            $data='';
            if($type)//type =1 histroy payout
            {
                $userid = $this->session->userdata('user')->id;//username,status,user_id
                // wall ->1 \\\ banner-> 2
                $data['content'] =
                    $this->load->view(
                        'mobile/content/his_payout',
                        array(
                             'pay'=>$this->Home_model->get_data('payout',array('iduser' => $userid),array(),array('id','DESC'))  
                             ),
                        true
                    ); 
            }
            else//history credit
            {
                $data['content'] =$this->load->view('mobile/content/his_credit',
                    array(
                    'pay'=>$this->Home_model->get_data('credit',array('iduser' => $this->session->userdata('user')->id),array(),array('id','DESC'))
                    ),true
                );
            }
             
            
                    
        }else {
            $this->page = 'history';
            $this->content = 'Not login';
             
        }
        $this->load->view('index',$data);
        
    }
   
    /////////////////////////////////////////////
    function phantrang(){
        $this->load->library('pagination');// da load ben tren
        $config['base_url'] = base_url().$this->uri->segment(1,0).'/'.$this->uri->segment(2,0).'/'.$this->uri->segment(3,0).'/'.$this->uri->segment(4,0).'/';
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 5;
        $config['num_links'] = 10;
        $config['first_link'] = '<<';
        $config['first_tag_open'] = '<li class="firt_page">';//div cho chu <<
        $config['first_tag_close'] = '</li>';//div cho chu <<
        $config['last_link'] = '>>';
        $config['last_tag_open'] = '<li class="last_page">';
        $config['last_tag_close'] = '</li>';
        //-------next-
        $config['next_link'] = 'next &gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        //------------preview
        $config['prev_link'] = '&lt; prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
       // ------------------cu?npage
        $config['cur_tag_open'] = '<li class="current">';
        $config['cur_tag_close'] = '</li>';
        //--so 
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        //-----
        $this->pagination->initialize($config);
    
    }
    
}